#################################################################################
# Estimating Dynamic Ideal Points for State Supreme Courts                      #
# Jason Windett, Jeffrey J. Harden, and Matthew E.K. Hall                       #
# Dynamic ideal point plots                                                     #
# Last update: 4/15/15                                                          # 
#################################################################################
### Packages and Data ###
library(foreign)
library(ggplot2)
library(scales)
library(grid)
library(gridExtra)
library(RColorBrewer)

ip <- read.dta("ip-plots.dta")
ip$Name <- ip$name
ip$State <- ip$stateab2

sn <- sort(unique(ip$stateab2))

ip$statenum <- NA
for(i in 1:length(sn)) ip$statenum[ip$stateab2 == sn[i]] <- i 

## Boxplot of state court IPs ##
f <- function(x) {
  r <- quantile(x, probs = c(0, 0.25, 0.5, 0.75, 1))
  names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
  r
}

pdf("scaled-ips.pdf", height = 9)
 
ggplot(ip, aes(reorder(stateab2, scaledideal, median), scaledideal)) +
 stat_summary(fun.data = f, geom = "boxplot") +
 coord_flip() +
 ylab("Ideal Points") + xlab("Courts") +
 scale_y_continuous(breaks = pretty_breaks(n = 13)) +
 theme(axis.title.x = element_text(vjust = -.5), axis.title.y = element_text(vjust = 1.5))
 
dev.off()


## Print dynamic IPs for DE, MO, OH, WV ##
de <- ggplot(ip[ip$stateab2 == "DE", ], aes(year, scaledideal, colour = Name)) +
 geom_point() +
 geom_line(lwd = 1) + 
 geom_ribbon(aes(ymin = Lower, ymax = Upper), alpha = .1) +
 ylab("Ideal Points") + xlab("Year") +
 scale_x_continuous(breaks = pretty_breaks(n = 16)) +
 scale_y_continuous(breaks = pretty_breaks(n = 13)) +
 theme(axis.title.x = element_text(vjust = -.5, size = 15), axis.title.y = element_text(vjust = 1.5, size = 15), axis.text.x = element_text(angle = 45, hjust = 1, size = 12), axis.text.y = element_text(size = 12)) + 
 theme(legend.text = element_text(size = 15), legend.title = element_text(size = 17)) + labs(title = "(a) Delaware") +
 scale_color_brewer(type = "qual", palette = "Paired")

mo.d <- ip[ip$stateab2 == "MO", ]
cc.mo <- length(unique(mo.d$Name))
get.palette <- colorRampPalette(brewer.pal(12, "Paired"))

mo <- ggplot(mo.d, aes(year, scaledideal, colour = Name)) +
 geom_point() +
 geom_line(lwd = 1) + 
 geom_ribbon(aes(ymin = Lower, ymax = Upper), alpha = .1) +
 ylab("Ideal Points") + xlab("Year") +
 scale_x_continuous(breaks = pretty_breaks(n = 16)) +
 scale_y_continuous(breaks = pretty_breaks(n = 13)) +
 theme(axis.title.x = element_text(vjust = -.5, size = 15), axis.title.y = element_text(vjust = 1.5, size = 15), axis.text.x = element_text(angle = 45, hjust = 1, size = 12), axis.text.y = element_text(size = 12)) + 
 theme(legend.text = element_text(size = 15), legend.title = element_text(size = 17)) + labs(title = "(b) Missouri") +
 scale_color_manual(values = get.palette(cc.mo))
 
oh.d <- ip[ip$stateab2 == "OH", ]
cc.oh <- length(unique(oh.d$Name))
oh.d$Name[oh.d$Name == "Ebrown"] <- "Brown"
oh.d$Name[oh.d$Name == "Oconnor"] <- "O'Connor"
oh.d$Name[oh.d$Name == "Odonnell"] <- "O'Donnell"

oh <- ggplot(oh.d, aes(year, scaledideal, colour = Name)) +
 geom_point() +
 geom_line(lwd = 1) + 
 geom_ribbon(aes(ymin = Lower, ymax = Upper), alpha = .1) +
 ylab("Ideal Points") + xlab("Year") +
 scale_x_continuous(breaks = pretty_breaks(n = 16)) +
 scale_y_continuous(breaks = pretty_breaks(n = 13)) +
 theme(axis.title.x = element_text(vjust = -.5, size = 15), axis.title.y = element_text(vjust = 1.5, size = 15), axis.text.x = element_text(angle = 45, hjust = 1, size = 12), axis.text.y = element_text(size = 12)) + 
 theme(legend.text = element_text(size = 15), legend.title = element_text(size = 17)) + labs(title = "(c) Ohio") +
 scale_color_manual(values = get.palette(cc.oh))

wv.d <- ip[ip$stateab2 == "WV", ]
cc.wv <- length(unique(wv.d$Name))
wv.d$Name[wv.d$Name == "Mccuskey"] <- "McCuskey"
wv.d$Name[wv.d$Name == "Mcgraw"] <- "McGraw"
wv.d$Name[wv.d$Name == "Mchugh"] <- "McHugh"

wv <- ggplot(wv.d, aes(year, scaledideal, colour = Name)) +
 geom_point() +
 geom_line(lwd = 1) + 
 geom_ribbon(aes(ymin = Lower, ymax = Upper), alpha = .1) +
 ylab("Ideal Points") + xlab("Year") +
 scale_x_continuous(breaks = pretty_breaks(n = 16)) +
 scale_y_continuous(breaks = pretty_breaks(n = 13)) +
 theme(axis.title.x = element_text(vjust = -.5, size = 15), axis.title.y = element_text(vjust = 1.5, size = 15), axis.text.x = element_text(angle = 45, hjust = 1, size = 12), axis.text.y = element_text(size = 12)) + 
 theme(legend.text = element_text(size = 15), legend.title = element_text(size = 17)) + labs(title = "(d) West Virginia") +
 scale_color_manual(values = get.palette(cc.wv))
 
pdf("scaled-dynamic-ips.pdf", width = 14, height = 12)

grid.arrange(de, mo, oh, wv, ncol = 2)  

dev.off()

## Print dynamic IPs for all state courts ##
ip.plot <- function(stateab){
st <- ip[ip$stateab2 == stateab, ]
cc <- length(unique(st$Name))

pdf(paste(stateab, ".pdf", sep = ""))

print(
ggplot(st, aes(year, scaledideal, colour = Name)) +
 geom_point() +
 geom_line(lwd = 1) + 
 geom_ribbon(aes(ymin = Lower, ymax = Upper), alpha = .1) +
 ylab("Ideal Points") + xlab("Year") +
 scale_x_continuous(breaks = pretty_breaks(n = 16)) +
 scale_y_continuous(breaks = pretty_breaks(n = 13)) +
 theme(axis.title.x = element_text(vjust = -.5, size = 15), axis.title.y = element_text(vjust = 1.5, size = 15), axis.text.x = element_text(angle = 45, hjust = 1, size = 12), axis.text.y = element_text(size = 12)) + 
 theme(legend.text = element_text(size = 15), legend.title = element_text(size = 17)) +
 scale_color_manual(values = get.palette(cc))
)
dev.off()

}

stateab <- unique(ip$stateab2)
for(i in 1:length(stateab)) ip.plot(stateab[i])


